﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Prime.Models;

namespace Prime.Controllers
{
    [Authorize]
    public class LessonQuestionsController : ApiController
    {
        private LearningDbEntities db = new LearningDbEntities();

        // GET api/LessonQuestions
        public IQueryable<LessonQuestion> GetLessonQuestions()
        {
            return db.LessonQuestions;
        }

        // GET api/LessonQuestions/5
        [ResponseType(typeof(LessonQuestion))]
        public IHttpActionResult GetLessonQuestion(int id)
        {
            LessonQuestion lessonquestion = db.LessonQuestions.Find(id);
            if (lessonquestion == null)
            {
                return NotFound();
            }

            return Ok(lessonquestion);
        }

        // PUT api/LessonQuestions/5
        public IHttpActionResult PutLessonQuestion(int id, LessonQuestion lessonquestion)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != lessonquestion.LessonId)
            {
                return BadRequest();
            }

            db.Entry(lessonquestion).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LessonQuestionExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/LessonQuestions
        [ResponseType(typeof(LessonQuestion))]
        public IHttpActionResult PostLessonQuestion(LessonQuestion lessonquestion)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.LessonQuestions.Add(lessonquestion);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (LessonQuestionExists(lessonquestion.LessonId))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = lessonquestion.LessonId }, lessonquestion);
        }

        // DELETE api/LessonQuestions/5
        [ResponseType(typeof(LessonQuestion))]
        public IHttpActionResult DeleteLessonQuestion(int id)
        {
            LessonQuestion lessonquestion = db.LessonQuestions.Find(id);
            if (lessonquestion == null)
            {
                return NotFound();
            }

            db.LessonQuestions.Remove(lessonquestion);
            db.SaveChanges();

            return Ok(lessonquestion);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool LessonQuestionExists(int id)
        {
            return db.LessonQuestions.Count(e => e.LessonId == id) > 0;
        }
    }
}